﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{ 
    public class GamesController : BaseController
    {
        private GrenadiersContext db = new GrenadiersContext();

        //
        // GET: /Games/

        public ViewResult Index()
        {
            return View(db.Games.ToList());
        }

        public ActionResult Qtip(int id)
        {
            Game game = db.Games.Find(id);
            return PartialView(game);
        }

        //
        // GET: /Games/Create

        public ActionResult Create()
        {
            ViewBag.Teams = new SelectList(db.Teams.Where(m => m.Year == 2013), "Id", "Name");
            return PartialView();
        } 

        //
        // POST: /Games/Create

        [HttpPost]
        public ActionResult Create(Game game, int HomeTeam_Id, int AwayTeam_Id)
        {
            game.UserCreatedBy = ViewBag.CurrentUser;
            game.DateCreated = DateTime.Now;
            game.UserModifiedBy = ViewBag.CurrentUser;
            game.DateModified = DateTime.Now;

            game.HomeTeam = db.Teams.Find(HomeTeam_Id);
            game.AwayTeam = db.Teams.Find(AwayTeam_Id);

            ModelState.Remove("UserCreatedBy");
            ModelState.Remove("UserModifiedBy");
            ModelState.Remove("HomeTeam");
            ModelState.Remove("AwayTeam");

            if (ModelState.IsValid)
            {
                db.Games.Attach(game);
                db.Games.Add(game);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.Teams = new SelectList(db.Teams.Where(m => m.Year == 2013), "Id", "Name");
            return PartialView(game);
        }
        
        //
        // GET: /Games/Edit/5
 
        public ActionResult Edit(int id)
        {
            Game game = db.Games.Find(id);

            ViewBag.Teams = new SelectList(db.Teams.Where(m => m.Year == 2013), "Id", "Name");
            return PartialView(game);
        }

        //
        // POST: /Games/Edit/5

        [HttpPost]
        public ActionResult Edit(Game game, int HomeTeam_Id, int AwayTeam_Id)
        {
            Game gamedb = db.Games.Find(game.Id);

            game.UserModifiedBy = ViewBag.CurrentUser;
            game.DateModified = DateTime.Now;

            TryUpdateModel(gamedb);

            gamedb.HomeTeam = db.Teams.Find(HomeTeam_Id);
            gamedb.AwayTeam = db.Teams.Find(AwayTeam_Id);

            ModelState.Remove("UserCreatedBy");
            ModelState.Remove("UserModifiedBy");
            ModelState.Remove("HomeTeam");
            ModelState.Remove("AwayTeam");


            if (ModelState.IsValid)
            {
                db.Events.Attach(gamedb);
                db.Entry(gamedb).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Teams = new SelectList(db.Teams.Where(m => m.Year == 2013), "Id", "Name");
            return PartialView(game);
        }

        //
        // GET: /Games/Delete/5
 
        public ActionResult Delete(int id)
        {
            Game game = db.Games.Find(id);
            return PartialView(game);
        }

        //
        // POST: /Games/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Game game = db.Games.Find(id);
            db.Events.Remove(game);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}